package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.CorrectShare;
import com.example.springsecurity.domain.ExportScore;
import com.example.springsecurity.domain.ShareProject;
import com.example.springsecurity.domain.TeacherCorrectShare;
import com.example.springsecurity.mapper.CorrectShareProjectMapper;
import com.example.springsecurity.service.CorrectShareProjectService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CorrectShareProjectServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-10-16 10:55
 **/
@Service
public class CorrectShareProjectServiceImpl implements CorrectShareProjectService {

    @Autowired
    private CorrectShareProjectMapper correctShareProjectMapper;

    /**
     * 获取用户(负责人/教师)负责的共享实验项目信息
     * @param userId   用户id
     * @param message  搜索条件
     * @return
     */
    @Override
    public List<ShareProject> getAllShareProject(Long userId, String message) {
        List<ShareProject> shareProjects = correctShareProjectMapper.getAllShareProject(userId,message);
        /**
         * 获取未批改
         */
        for(ShareProject shareProject:shareProjects){
           Integer count = correctShareProjectMapper.getNoCorrectCount(shareProject.getProjectId());
           shareProject.setCount(count);
        }
        return shareProjects;
    }


    /**
    * 获取到批改列表
    * @param projectId  项目实验id
    * @param realName   学生真实姓名
    * @param correctStatus  提交状态
    * @return
    */
    @Override
    public List<TeacherCorrectShare> getAllTeacherCorrectShare(Integer projectId, String realName, Integer correctStatus) {
        return correctShareProjectMapper.getAllTeacherCorrectShare(projectId,realName,correctStatus);
    }

    /**
     * 教师批改学生共享 点击批改后返回的内容
     * @param submitId
     * @return
     */
    @Override
    public CorrectShare getCorrectShare(Integer submitId) {
        return correctShareProjectMapper.getCorrectShare(submitId);
    }

    /**
     * 教师批改共享，提交批改成绩
     * @param teacherCorrectShare
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertCorrectShare(TeacherCorrectShare teacherCorrectShare) {
        /**
         * 先改变共享实验成绩表的状态
         */
        correctShareProjectMapper.updateCorrectStatus(teacherCorrectShare.getSubmitId());
        /**
         * 在执行批改表成绩插入
         */
        return correctShareProjectMapper.insertCorrectShare(teacherCorrectShare);
    }

    /**
     * 根据批改id获取批改详情
     * @param correctId
     * @return
     */
    @Override
    public TeacherCorrectShare getTeacherCorrectShareById(Integer correctId) {
        return correctShareProjectMapper.getTeacherCorrectShareById(correctId);
    }

    /**
     * 导出成绩
     * @param projectId
     * @return
     */
    @Override
    public List<ExportScore> exportCorrectShare(Integer projectId) {
        return correctShareProjectMapper.exportCorrectShare(projectId);
    }


}
